package com.sdkj.seckill.rocketmqTest.delayMessage;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Description 延时消息消费者
 * @Author Jiang
 * @Date 2022/01/18 20:51
 * @Version 1.0.0
 */
//启动消费者等待传入订阅消息
public class ScheduledMessageConsumer {
    public static void main(String[] args) throws Exception {
        //实例化消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ExampleConsumer");
        consumer.setNamesrvAddr("47.94.172.125:9876");
        //订阅Topics
        consumer.subscribe("TestTopic", "*");
        //注册消息监听者
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list,
                                                            ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt message : list) {
                    //输出近似的延时间隔
                    System.out.println("Receive message[mshId=" + message.getMsgId() + "]" +
                            (System.currentTimeMillis() - message.getBornTimestamp()) + "ms later");
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

            }
        });

        //启动消费者
        consumer.start();

    }
}
